package com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.entity.vo.SmsTeachAdministrativeStaffCancelVO;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.entity.vo.SmsTeachAdministrativeStaffCheckInApplicationVO;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.entity.vo.SmsTeachApplicationExportVO;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.entity.vo.SmsTeachStaffAllowVO;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.service.SmsTeachAdministrativeStaffCheckInApplicationService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-05-05
 */
@RestController
@SecretBody
@RequestMapping("/teachAndStaffCheckInApplication/smsTeachAdministrativeStaffCheckInApplication")
public class SmsTeachAdministrativeStaffCheckInApplicationController {
    @Autowired
    SmsTeachAdministrativeStaffCheckInApplicationService smsTeachAdministrativeStaffCheckInApplicationService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("教职工入住申请新增")
    @PostMapping("/smsTeachAdministrativeStaffCheckInApplicationAdd")
    public ResponseResult smsTeachAdministrativeStaffCheckInApplicationAdd(
            @RequestBody SmsTeachAdministrativeStaffCheckInApplicationVO smsTeachAdministrativeStaffCheckInApplication,
            HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsTeachAdministrativeStaffCheckInApplicationService.smsTeachAdministrativeStaffCheckInApplicationAdd(
                smsTeachAdministrativeStaffCheckInApplication,
                user_id);
    }

    @ApiModelProperty(value = "撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelSmsTeachAdministrativeStaffCheckInApplication(@RequestBody SmsTeachAdministrativeStaffCancelVO cancelVO, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsTeachAdministrativeStaffCheckInApplicationService.cancelSmsTeachAdministrativeStaffCheckInApplication(cancelVO, user_id);
    }



    @ApiModelProperty("教职工查询入住申请")
    @GetMapping("/selectSmsTeachAdministrativeStaffCheckInApplication")
    public ResponseResult selectSmsTeachAdministrativeStaffCheckInApplication(
            @RequestParam(value = "application_state", required = false) Integer application_state,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "bg_date", required = false) String bg_date,
            @RequestParam(value = "end_date", required = false) String end_date,
            HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsTeachAdministrativeStaffCheckInApplicationService.selectSmsTeachAdministrativeStaffCheckInApplication(
                application_state, pageSize, pageNum, bg_date, end_date, user_id);
    }

    @ApiOperation("教职工根据id查询入住申请")
    @GetMapping("/selectSmsTeachAdministrativeStaffCheckInApplicationById")
    public ResponseResult selectSmsTeachAdministrativeStaffCheckInApplicationById(
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) {
        return smsTeachAdministrativeStaffCheckInApplicationService.selectSmsTeachAdministrativeStaffCheckInApplicationById(
                id,
                pageSize,
                pageNum);
    }

    @ApiOperation("审批人根据id查询教职工入住申请")
    @GetMapping("/selectByIdAllowUser")
    public ResponseResult selectByIdAllowUser(@RequestParam(value = "id", required = false) String id,
                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) {
        return smsTeachAdministrativeStaffCheckInApplicationService.selectByIdAllowUser(id, pageSize, pageNum);
    }

    @ApiOperation("审批人查询教职工入住申请")
    @GetMapping("/selectSmsTeachAdministrativeStaffCheckInApplicationByAllowUser")
    public ResponseResult selectSmsTeachAdministrativeStaffCheckInApplicationByAllowUser(@RequestParam(value = "teachStaffName", required = false) String teachStaffName,
                                                                                         HttpServletRequest request,
                                                                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                                         @RequestParam(value = "bg_date", required = false) String bg_date,
                                                                                         @RequestParam(value = "end_date", required = false) String end_date
    ) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsTeachAdministrativeStaffCheckInApplicationService.selectSmsTeachAdministrativeStaffCheckInApplicationByAllowUser(
                teachStaffName,
                user_id,
                pageSize,
                pageNum,
                bg_date,
                end_date);
    }

    @ApiOperation("审批")
    @PostMapping("/allowUser/allow")
    public ResponseResult smsTeachAdministrativeStaffCheckInApplicationByAllow(@RequestBody SmsTeachStaffAllowVO smsTeachStaffAllowVO,
                                                                               HttpServletRequest request) {
        String allow_person_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsTeachAdministrativeStaffCheckInApplicationService.smsTeachAdministrativeStaffCheckInApplicationByAllow(
                smsTeachStaffAllowVO,
                allow_person_id);
    }

    @ApiModelProperty(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult smsTeachAdministrativeStaffCheckInApplicationByAllowList(
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "search_str",required = false) String search_str,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "application_type") Integer application_type,
            HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsTeachAdministrativeStaffCheckInApplicationService.smsTeachAdministrativeStaffCheckInApplicationByAllowList(user_id, application_type, pageNum, pageSize,search_str);
    }

    @ApiModelProperty(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult getTeachAdministrativeStaffCheckInApplicationDetail(@RequestParam(value = "application_id") String application_id,
                                                                              HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsTeachAdministrativeStaffCheckInApplicationService.getTeachAdministrativeStaffCheckInApplicationDetail(application_id, user_id);
    }

    @ApiModelProperty(value = "申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getTeachAdministrativeStaffCheckInApplicationProcessList(){
        return smsTeachAdministrativeStaffCheckInApplicationService.getTeachAdministrativeStaffCheckInApplicationProcessList();
    }
    @ApiModelProperty(value = "文件下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "apply_id") String apply_id,
                                        HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsTeachAdministrativeStaffCheckInApplicationService.tableDownload(apply_id,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
    @ApiModelProperty(value = "查询所有申请记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllApply(
            @RequestParam(value = "apply_unit",required = false) String apply_unit,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum
    ){
        return smsTeachAdministrativeStaffCheckInApplicationService.selectAllApply(apply_unit,
                pageSize,pageNum);
    }
    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportTeachApplication(@RequestBody SmsTeachApplicationExportVO exportVO,
                                                    HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsTeachAdministrativeStaffCheckInApplicationService.exportTeachApplication(exportVO, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
}

